﻿using Lg.Core;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;
using System.Text;
using Lg.Core.Extensions;
using Microsoft.Extensions.Caching.Memory;

using Lg.Core.RidAuthor;
using Lg.Core.Exceptions;
using Survey.Core;
using Survey.Models;


namespace Survey.Web.Filters
{
    /// <summary>
    /// 管理员登陆验证特性
    /// </summary>
    public class AuthorAttribute : Attribute, IAuthorizationFilter
    {
        string? toUrl;
        string? moduleSN;
        string? powerKey;
        public AuthorAttribute(string? moduleSN = null, string? powerKey = null, string? toUrl = null)
        {
            this.toUrl = toUrl;
            this.moduleSN = moduleSN;
            this.powerKey = powerKey;
        }
        public void OnAuthorization(AuthorizationFilterContext context)
        {
            string? sessionData = context.HttpContext.Session.GetString("LoginAdmin");
            
            var admin = sessionData?.ToModel<Admin>();
            if (admin != null)
            {
                //if (admin.SuperKey == null || admin.SuperKey != system.cert?.SuperKey)//不是超管
                //{
                //    if (system.cert == null || system.cert.EndDate <= DateTime.Now)
                //    {
                //        context.HttpContext.Session.Remove("LoginAdmin");
                //        context.Result = new ContentResult { Content = new RtMsg() { code = 999, msg = "预授权已过期！" }.ToJsonString() };
                //    }
                //    if (moduleSN != null)//较验权限列表中是否有指定SN的模块
                //    {
                //        model.ext.SysModule? module = system.cert?.Modules?.FirstOrDefault(p => p.SN == moduleSN);
                //        if (module != null)
                //        {
                //            model.ext.PowerItem? item = admin.Powers.ToList<model.ext.PowerItem>()?.FirstOrDefault(p => p.Id == module.Id);
                //            if (item == null)
                //            {
                //                // throw new LgException("没有权限！");
                //                context.Result = new ContentResult { Content = new RtMsg() { code = 111, msg = "没有权限！" }.ToJsonString() };
                //            }
                //            else if (powerKey != null)
                //            {
                //                bool b = item.Powers!.Contains(powerKey);
                //                if (!b)
                //                {
                //                    context.Result = new ContentResult { Content = new RtMsg() { code = 111, msg = "没有权限！" }.ToJsonString() };
                //                }
                //            }
                //        }
                //        else
                //        {
                //            context.Result = new ContentResult { Content = new RtMsg() { code = 111, msg = "没有权限！" }.ToJsonString() };
                //            //throw new LgException($"指定模块:{moduleSN}不在授权列表内，请联系管理员确认编号是否有误!");
                //        }
                //    }
                //}
            }
            else
            {
                if (toUrl != null)
                {
                    context.Result = new RedirectResult(toUrl);
                }
                else
                {
                    context.Result = new ContentResult { Content = new RtMsg() { code = 110, msg = "未登陆或登陆超时！" }.ToJsonString() };
                }

            }
        }
    }
}
